﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Newtonsoft.Json;
using System;

namespace lwzlz.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class SMSHelp
    {
        /// <summary>
        /// 发送手机短信
        /// </summary>
        /// <param name="mobile">手机号</param>
        /// <param name="message">短信内容</param>
        /// <returns></returns>
        public static void PostMsg(string mobile, string message, string templateCode)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAI4G9aDphUfnUUHbEje2p8", "RgXD8Qsn3GnRhCqdWqw0SzNocJ3uiZ");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", mobile);
            request.AddQueryParameters("SignName", "陆维钊练字");
            request.AddQueryParameters("TemplateCode", templateCode);
            request.AddQueryParameters("TemplateParam", message);
            try
            {
                CommonResponse response = client.GetCommonResponse(request);

                var msg = JsonConvert.DeserializeObject<Msg>(response.Data);
                if (msg.Code != "OK")
                {
                    throw new Exception(msg.Message);
                }
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }
        }
    }

    /// <summary>
    /// 返回信息
    /// </summary>
    public class Msg
    {
        /// <summary>
        /// 信息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string RequestId { get; set; }
        /// <summary>
        /// 编号
        /// </summary>
        public string Code { get; set; }
    }
}
